package the.broccoli.ac.controller;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.constant.WebConsts;
import the.broccoli.ac.dto.ResCondition;
import the.broccoli.ac.dto.ResTree;
import the.broccoli.ac.entity.Res;
import the.broccoli.ac.service.IResService;
import the.broccoli.ac.vo.ResTreeListVO;
import the.broccoli.auth.UserContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 资源控制器
 *
 * @author lei.liu
 * @since 2020-04-07 14:03:38
 */
@RestController
@RequestMapping("/ac/res")
public class ResController {

    @Autowired
    private IResService resService;

    @PostMapping("/add")
    public APIResult<Res> add(
            @ApiParam(name = "appId", value = "应用ID", required = true)
            @RequestParam(name = "appId", required = true) Integer appId,
            @ApiParam(name = "parentId", value = "父级ID", required = true)
            @RequestParam(name = "parentId", required = true) String parentId,
            @ApiParam(name = "resName", value = "资源名称", required = true)
            @RequestParam(name = "resName", required = true) String resName,
            @ApiParam(name = "resType", value = "资源类型。DIR：目录；MENU：菜单；ACTION：操作；PAGE：页面", required = true)
            @RequestParam(name = "resType", required = true) String resType,
            @ApiParam(name = "url", value = "链接", required = false)
            @RequestParam(name = "url", required = false) String url,
            @ApiParam(name = "descr", value = "描述", required = false)
            @RequestParam(name = "descr", required = false) String descr,
            @ApiParam(name = "serial", value = "排列序号", required = false)
            @RequestParam(name = "serial", required = false) Integer serial,
            @ApiParam(name = "resCode", value = "编码", required = true)
            @RequestParam(name = "resCode", required = true) String resCode
    ) {
        Res entity = new Res();
        entity.setAppId(appId);
        entity.setParentId(parentId);
        entity.setResName(resName);
        entity.setUrl(url);
        entity.setResType(resType);
        entity.setDescr(descr);
        entity.setSerial(serial);
        entity.setDisabled(0);
        entity.setResCode(resCode);
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        resService.save(entity);
        return APIResult.success(entity);
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        resService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
            @ApiParam(name = "resId", value = "资源ID", required = true)
            @RequestParam(name = "resId", required = true) String resId,
            @ApiParam(name = "resName", value = "资源名称", required = true)
            @RequestParam(name = "resName", required = true) String resName,
            @ApiParam(name = "resType", value = "资源类型。DIR：目录；MENU：菜单；ACTION：操作；PAGE：页面", required = true)
            @RequestParam(name = "resType", required = true) String resType,
            @ApiParam(name = "url", value = "链接", required = false)
            @RequestParam(name = "url", required = false) String url,
            @ApiParam(name = "descr", value = "描述", required = false)
            @RequestParam(name = "descr", required = false) String descr,
            @ApiParam(name = "serial", value = "排列序号", required = false)
            @RequestParam(name = "serial", required = false) Integer serial,
            @ApiParam(name = "resCode", value = "编码", required = true)
            @RequestParam(name = "resCode", required = true) String resCode
    ) {
        Res entity = new Res();
        entity.setResId(resId);
        entity.setResName(resName);
        entity.setUrl(url);
        entity.setResType(resType);
        entity.setDescr(descr);
        entity.setSerial(serial);
        entity.setDisabled(0);
        entity.setResCode(resCode);
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        resService.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{resId}")
    public APIResult get(@PathVariable(value = "resId") String resId) {
        return APIResult.success(resService.getById(resId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult> page(
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        ResCondition vo = new ResCondition();
        vo.setKeyword(keyword);
        vo.setOrderBy(orderBy);
        return APIResult.success(resService.pageList(vo, pageNum, pageSize));
    }

    @GetMapping("/tree/list/{appId}")
    public APIResult list(
            @ApiParam(name = "appId", value = "应用ID", required = true)
            @PathVariable(value = "appId")  Integer appId
    ) {
        List<ResTreeListVO> resTreeList = new ArrayList<>();
        ResCondition condition = new ResCondition();
        condition.setAppIdList(Lists.newArrayList(appId));
        List<Res> resList = resService.listByCondition(condition);

        if (!CollectionUtils.isEmpty(resList)) {
            ResTreeListVO resTree = null;
            for (Res res : resList) {
                resTree = new ResTreeListVO();
                resTree.setId(res.getResId());
                resTree.setPid(WebConsts.ROOT.equals(res.getParentId()) ? "0" : res.getParentId());
                resTree.setTitle(res.getResName());
                resTree.setUrl(res.getUrl());
                resTree.setSerial(res.getSerial());
                resTree.setDisabled(res.getDisabled());
                resTree.setResCode(res.getResCode());
                resTreeList.add(resTree);
            }
        }
        return APIResult.success(resTreeList);
    }

    @GetMapping("/tree/{appId}")
    public APIResult<List<ResTree>> getTree(
            @ApiParam(name = "appId", value = "应用ID", required = true)
            @PathVariable(value = "appId") Integer appId
    ) {
        return APIResult.success(resService.getTree(appId, null));
    }
}